package kr.saeildamil.androidstudty.ch13;

import java.util.Calendar;
import java.util.GregorianCalendar;

import kr.saeildamil.androidstudty.R;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class PickerDialig extends Activity {
	int mYear,mMonth,mDay,mHour,mMinute;
	TextView mDatetv;
	TextView mTimetv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch13_pickerdialog);
		
		mDatetv=(TextView) findViewById(R.id.ch13_pickerdialog_textdate);
		mTimetv=(TextView) findViewById(R.id.ch13_pickerdialog_texttime);
		
		Calendar cal=new GregorianCalendar();
		mYear=cal.get(Calendar.YEAR);
		mMonth=cal.get(Calendar.MONTH);
		mDay=cal.get(Calendar.DAY_OF_MONTH);
		mHour=cal.get(Calendar.HOUR_OF_DAY);
		mMinute=cal.get(Calendar.MINUTE);
		
		updateNow();
	}
	private void updateNow() {
		mDatetv.setText(String.format("%d/%d/%d", mYear, mMonth,mDay));
		mTimetv.setText(String.format("%d:%d", mHour,mMinute));		
	}
	public void mOnClick(View v){
		switch (v.getId()) {
		case R.id.ch13_pickerdialog_date_btn:
			new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay).show();
			break;
		case R.id.ch13_pickerdialog_time_btn:
			new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false).show();
			break;
		}
	}
	TimePickerDialog.OnTimeSetListener mTimeSetListener=new TimePickerDialog.OnTimeSetListener() {
		
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mHour=hourOfDay;
			mMinute=minute;
			updateNow();
			
		}
	};
	DatePickerDialog.OnDateSetListener mDateSetListener=new DatePickerDialog.OnDateSetListener() {
		
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear=year;
			mMonth=monthOfYear;
			mDay=dayOfMonth;
			updateNow();
			
		}
	};
}
